%GLOBAL CWD; %LET CWD = %SUBSTR(%SYSGET(SAS_EXECFILEPATH), 1, %EVAL(%LENGTH(%SYSGET(SAS_EXECFILEPATH)) - %LENGTH(%SYSGET(SAS_EXECFILENAME)) - 1));
%INCLUDE "&CWD\TOOLS.SAS";

/* SET ELI & CLUSTER */
%LET ELI = FH011;
%LET CLUSTER = 01;

/* IDENTIFYING HANDLE */
%LET COM = &ELI._&CLUSTER.;

/* ITEM-SPECIFIC RIGHT HAND SIDE VARIABLES */
%LET RHS= 
    C1 D1 E1 E2 E3 E4 F1 F2 F3  G1 G2 G3 G5
    MNAMT MNAMTSQ
    ;

%LET STYR=0201;
%LET FIRSTPER=20&STYR;
%LET STDUM=D&STYR;
%LET RHYRS= 
        D0202--D0709;
%LET NSPER=69;
%LET NVEC=84;

/* DEFINE ITEM-SPECIFIC PRICE EDITS */
%MACRO EDITS;
    IF CLUSTER="01";
    IF A="A1"; * CHICKEN EGGS;
    IF B="B1"; * SOLD IN STORE;
    *;
    *INSERT PRICE EDITS;
    *;
    LNPRICE=LOG(FULLSAMP_EFFPR_WITX);

    QTY=DER_QTY;
        IF DER_QTY>1 THEN DO;
            IF G='G1' THEN DO;
                IF DER_QTY IN(6 12) THEN QTY=DER_QTY;
                ELSE QTY=6*DER_QTY;         
                END;
            ELSE IF G='G2' THEN DO;
                IF DER_QTY=12 THEN QTY=12;
                ELSE QTY=12*DER_QTY;
                END;
            ELSE IF G='G3' THEN DO;
                IF DER_QTY=18 THEN QTY=18;
                ELSE QTY=18*DER_QTY;
                END;
            ELSE IF G='G6' THEN QTY=30*DER_QTY;
            ELSE IF G='G99' THEN DO;
                IF FIND(ZZ99,'3 DOZ') OR FIND(G99,'2PACK/1 AND 1/2 DOZ') OR FIND(G99,'2 CARTONS-18 EGGS EA') THEN QTY=36;
                ELSE IF DER_QTY=2 AND FIND(G99,'TRAY OF 30') THEN QTY=60;
                ELSE IF DER_QTY=4 AND FIND(G99,'90 EGGS') THEN QTY=90;
                END;
            END;
    *;
    DER_QTY=QTY;

    IF .04>DER_PRICE OR DER_PRICE>10 THEN DELETE;  *OUTLIERS REMOVAL;
    LNAMT =LOG(DER_SIZE*DER_QTY);

    IF LNAMT =. THEN DELETE;
    *;
    IF OUTLET_CD="1789205" AND QT_CD="037" THEN DELETE;
    IF OUTLET_CD="1789205" AND QT_CD="038" THEN DELETE;
    IF OUTLET_CD="3202205" AND QT_CD="043" THEN DELETE;
    IF OUTLET_CD="3279957" AND QT_CD="019" THEN DELETE;
    IF OUTLET_CD="3356570" AND QT_CD="110" THEN DELETE;
    IF OUTLET_CD="3622572" AND QT_CD="029" THEN DELETE;
    IF OUTLET_CD="4240505" AND QT_CD="016" THEN DELETE;
    IF OUTLET_CD="5304884" AND QT_CD="001" THEN DELETE;
    IF OUTLET_CD="5304884" AND QT_CD="002" THEN DELETE;
    IF OUTLET_CD="5339925" AND QT_CD="010" THEN DELETE;
    IF OUTLET_CD="5427341" AND QT_CD="001" THEN DELETE;
    IF OUTLET_CD="5427476" AND QT_CD="002" THEN DELETE;
    IF OUTLET_CD="5632908" AND QT_CD="001" THEN DELETE;
    IF OUTLET_CD="5745609" AND QT_CD="015" THEN DELETE;
    IF OUTLET_CD="612552-" AND QT_CD="003" THEN DELETE;
    IF OUTLET_CD="612552-" AND QT_CD="004" THEN DELETE;
    IF OUTLET_CD="6326044" AND QT_CD="001" THEN DELETE;
    IF OUTLET_CD="6326044" AND QT_CD="002" THEN DELETE;
    IF DER_QTY GE 12 THEN LNAMT = LNAMT - LOG(12);
    IF LNPRICE LT LOG(.50) THEN DELETE;
    IF LNPRICE GT LOG(5) THEN DELETE;
    IF LNAMT GT LOG(4) THEN DELETE;
%MEND;

/* DEFINE ITEM-SPECIFIC DUMMIES */
%MACRO VARS;
    H99SP=(H="H99");

    ARRAY SPECARRAY (*) C1 C2 C99SP 
        D1 D2 D3 D99SP E1 E2 E3 E4 E5 E99SP F1 F2 F3 F99SP G1 G2 G3 G4 G5 G6 G7 H99SP
        DISCDEP--OTHSTORE;
    DO COUNT=1 TO DIM(SPECARRAY);
        IF SPECARRAY(COUNT)=. THEN DELETE;
    END;
%MEND;

LIBNAME IN "&CWD\DATA";
LIBNAME OUT "&CWD\DATA";

FILENAME OUTLOGS "&CWD\OUTPUT\LOGINDEXES.XLS";
FILENAME OUTCONTS "&CWD\OUTPUT\LOGCONTRIBS.XLS";
FILENAME OUTDISCS "&CWD\OUTPUT\DISCOUNTS.XLS";
FILENAME OUTPCTLS "&CWD\OUTPUT\PCTLS.XLS";

%INCLUDE "&CWD\TABLESTOBUILD.SAS";
